﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FormBase;
using System.Drawing.Drawing2D;

namespace FormDialog
{
    public partial class FrmDialog : FrmBase
    {
        private FrmDialog(string strMessage,
            string strTitle,
            bool blnShowCancel = false,
            bool blnShowClose = false,
            string Btn1_Text="确定",
                string Btn2_Text="取消")
        {
            InitializeComponent();
            InitFormMove(this.lblTitle);
            if (!string.IsNullOrWhiteSpace(strTitle))
                lblTitle.Text = strTitle;
            lblMsg.Text = strMessage;
            btnOK.BtnText = Btn1_Text;
            btnCancel.BtnText = Btn2_Text;
            if (blnShowCancel)
            {
                this.tableLayoutPanel1.ColumnStyles[1].Width = 1;
                this.tableLayoutPanel1.ColumnStyles[2].Width = 50;
            }
            else
            {
                this.tableLayoutPanel1.ColumnStyles[1].Width = 0;
                this.tableLayoutPanel1.ColumnStyles[2].Width = 0;
            }
            btnClose.Visible = blnShowClose;
        }

        #region 显示一个模式信息框
        /// <summary>
        /// 显示一个提示框
        /// </summary>
        /// <param name="owner">调用窗体</param>
        /// <param name="strMessage">提示内容</param>
        /// <param name="strTitle">提示框标题</param>
        /// <param name="blnShowCancel">是否显示取消按钮</param>
        /// <param name="blnShowClose">是否显示关闭按钮</param>
        /// <param name="deviationSize">大小偏移，当默认大小过大或过小时，可以进行调整（增量）</param>
        /// <param name="Btn1_Text">按钮1文本</param>
        /// <param name="Btn2_Text">按钮2文本</param>
        /// <returns>返回值</returns>
        public static DialogResult ShowDialog(
            IWin32Window owner,
            string strMessage,
            string strTitle = "提示",
            bool blnShowCancel = false,
            bool blnShowClose = false,
            Size? deviationSize = null,
            string Btn1_Text = "确定",
                string Btn2_Text = "取消")
        {
            DialogResult result = DialogResult.Cancel;
            if (owner == null || (owner is Control && (owner as Control).IsDisposed))
            {
                var frm = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, Btn1_Text, Btn2_Text)
                {
                    StartPosition = FormStartPosition.CenterScreen,
                    TopMost = true
                };
                if (deviationSize != null)
                {
                    frm.Width += deviationSize.Value.Width;
                    frm.Height += deviationSize.Value.Height;
                }
                result = frm.ShowDialog();
            }
            else
            {
                if (owner is Control)
                {
                    owner = (owner as Control).FindForm();
                }
                var frm = new FrmDialog(strMessage, strTitle, blnShowCancel, blnShowClose, Btn1_Text, Btn2_Text)
                {
                    StartPosition = (owner != null) ? FormStartPosition.CenterParent : FormStartPosition.CenterScreen,
                    TopMost = true
                };
                if (deviationSize != null)
                {
                    frm.Width += deviationSize.Value.Width;
                    frm.Height += deviationSize.Value.Height;
                }
                result = frm.ShowDialog(owner);
            }
            return result;
        }
        #endregion

        private void btnOK_BtnClick(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_BtnClick(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnClose_MouseDown(object sender, MouseEventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        //按钮响应回车键
        private void FrmDialog_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
            {
                if (btnOK.Focused)
                    btnOK_BtnClick(sender, null);
                else if (btnCancel.Focused)
                    btnCancel_BtnClick(sender, null);
            }           
        }

        //显示下划线以提供按钮获得焦点信息
        private void btnOK_GotFocus(object sender,EventArgs e)
        {
            btnOK.BtnFont = new Font(btnOK.BtnFont, FontStyle.Underline);
        }

        private void btnOK_LostFocus(object sender,EventArgs e)
        {
            btnOK.BtnFont = new Font(btnOK.BtnFont, FontStyle.Regular);
        }

        private void btnCancel_GotFocus(object sender, EventArgs e)
        {
            btnCancel.BtnFont = new Font(btnCancel.BtnFont, FontStyle.Underline);
        }

        private void btnCancel_LostFocus(object sender, EventArgs e)
        {
            btnCancel.BtnFont = new Font(btnCancel.BtnFont, FontStyle.Regular);
        }

        private void FrmDialog_Load(object sender, EventArgs e)
        {
            btnOK.GotFocus += btnOK_GotFocus;
            btnOK.LostFocus += btnOK_LostFocus;
            btnCancel.GotFocus += btnCancel_GotFocus;
            btnCancel.LostFocus += btnCancel_LostFocus;
            btnOK.Focus();
        }
    }
}
